package org.oa.ai.service;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import dev.langchain4j.agent.tool.P;
import dev.langchain4j.agent.tool.Tool;
import org.oa.ai.chatModel.CcCtCwyjyy;
import org.oa.ai.chatModel.OactbdCsqjsq;
import org.oa.ai.feign.AIFeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class YyToolService {

    @Autowired
    private AIFeignService aiFeignService;


    @Tool(value = "保存用印申请", name = "submitYysq")
    public Map<String, Object> submitQjsq(
            @P("用印事由") String yjsy,
            @P("申请部门") String sqbm,
            @P("申请人") String sqr,
            @P("申请日期") String sqrq,
            @P("联系电话") String lxdh,
            @P("受文单位") String swdw,
            @P("财务专用章") Integer cwzyz,
            @P("人名章") Integer rmz,
            @P("账号id") String userId,
            @P("部门id") String deptId,
            @P("当前部门") String loginDeptName
    ) {
//        String genId = IdUtil.simpleUUID();
        CcCtCwyjyy ccCtCwyjyy = new CcCtCwyjyy();
//        ccCtCwyjyy.setId(genId);
        ccCtCwyjyy.setTitle(yjsy+"【"+sqrq+"】");
        ccCtCwyjyy.setYysy(yjsy);
        ccCtCwyjyy.setSqr(sqr);
        ccCtCwyjyy.setSqrq(sqrq);
        ccCtCwyjyy.setSqbm(sqbm);
        ccCtCwyjyy.setLxdh(lxdh);
        ccCtCwyjyy.setSwdw(swdw);
        ccCtCwyjyy.setCwzyz(cwzyz);
        ccCtCwyjyy.setRmz(rmz);
        ccCtCwyjyy.setRmz(rmz);
        ccCtCwyjyy.setCreateuserid(userId);
        ccCtCwyjyy.setCreatedeptname(loginDeptName);
        ccCtCwyjyy.setCreatedeptid(deptId);
//        ccCtCwyjyy.setCreateTime(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        ccCtCwyjyy.setMjid("10");
        ccCtCwyjyy.setMjname("公开");
        ccCtCwyjyy.setYwlx("xzcwyjyy2");
        ccCtCwyjyy.setYwname("印鉴用印");
        ccCtCwyjyy.setStatus("10");
        Map<String, Object> bean = JSONUtil.toBean(JSONUtil.toJsonStr(ccCtCwyjyy), Map.class);
        try {
            Map<String, Object> result = aiFeignService.addForm(bean);
            return Map.of(
                    "新增后的表单数据", JSONUtil.toBean(JSONUtil.toJsonStr(result.get("formData")), CcCtCwyjyy.class),
                    "表名称", result.get("tableName")
            );
        }catch (Exception e) {
//            return Map.of("result", "保存成功！");
            return Map.of("result", "保存失败！");
        }
    }

//    @Tool("查询用印申请数据")
//    public OactbdCsqjsq selectQjById(@P("qjId") String qjId) {
//        OactbdCsqjsq map = aiFeignService.getDataById(qjId);
//        return map;
//    }


    @Tool("获取用印流程节点")
    public JSONArray getFlowNodeList(@P("印鉴申请id") String id, @P("表名称") String tableName,  @P("当前用户id") String login_user_id) {
        // 通过请假数据id查询数据
        Map<String, Object> map = aiFeignService.getDataByIdByCC(tableName, id);
        String ywlx = map.get("ywlx").toString();
        JSONArray obj = aiFeignService.nextTasksByNodeIdCC("", ywlx, login_user_id);
        return obj;
    }

    @Tool("获取流程节点人员列表")
    public JSONArray getFlowUserByNodeIdNew (@P("登录用户id") String userId, @P("流程节点id") String nodeId) {
        JSONArray userList = aiFeignService.getFlowUserByNodeId(nodeId, "csqjsq", userId, userId);
        return userList;
    }

    @Tool("提交用印流程")
    public String taskComplete(
            @P("流程节点id") String nodeId,
            @P("流程节点名称") String nodeName,
            @P("提交的用户id") String userIds,
            @P("提交的用户名称") String userNames,
            @P("qjId") String qjId
    ) {
        OactbdCsqjsq oactbdCsqjsq = aiFeignService.getDataById(qjId);
        // 调用流程启动接口
        String businessKey = oactbdCsqjsq.getId();
        String processDefinitionKey = oactbdCsqjsq.getYwlx();
        String title = oactbdCsqjsq.getTitle();
        String moduleId = oactbdCsqjsq.getYwlx();
        String firstNode = nodeId;
        String reportStatus = "ptyg";
        String mijiid = oactbdCsqjsq.getMjid();
        String tableName = "";
        String comminfoId = "";
        String formTyp = oactbdCsqjsq.getFormtype();
        // 流程启动
        Map<String, Object> flowStartMap = aiFeignService.startProcessByKeyNew(businessKey, processDefinitionKey, title, moduleId, firstNode, reportStatus, mijiid, tableName, comminfoId, oactbdCsqjsq.getCreateuserid(), formTyp);
        // 获取任务信息
        Map<String, Object> taskInfo = (Map<String, Object>) flowStartMap.get("taskInfo");
        if (taskInfo != null) {
            String taskId = taskInfo.get("taskId").toString();
            // 修改流程状态
            Boolean b = aiFeignService.updateReportStatus(taskId, "tjz");
            // 如果修改成功，则流程提交
            if (b) {
                String currentTaskId = taskId;
                String nextTaskActivityId = nodeId;
                String nextTaskUserIds = userIds;
                String nextTaskUsernames = userNames;
                String assigneeId = userIds;
                String nodeCode = "";
                String rwlxs = "ptyg";
                String exField5 = "true";
                String processDefinitionId = "";
                String currentNodeId = "";
                String tjlx = "";
                String logtype = "";
                // 提交流程
                Map<String, Object> map = aiFeignService.taskCompleteNew(
                        businessKey,
                        currentTaskId,
                        nextTaskActivityId,
                        nextTaskUserIds,
                        nextTaskUsernames,
                        assigneeId,
                        moduleId,
                        nodeName,
                        reportStatus,
                        nodeCode,
                        rwlxs,
                        exField5,
                        processDefinitionId,
                        currentNodeId,
                        tjlx,
                        logtype,
                        tableName,
                        comminfoId,
                        processDefinitionKey,
                        formTyp,
                        oactbdCsqjsq.getCreateuserid()
                );
            }
        }
        return "流程已经提交";
    }

}
